﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration;
using Castle.Windsor.Configuration.Interpreters;

namespace Bosphorus.Migrator.Facade
{
    public class IoC
    {
        private const string IOC_FULL_PATH = "Config/IoC.Facade.xml";
        private static readonly IWindsorContainer container;

        static IoC()
        {
            string streamString = GetContent(IOC_FULL_PATH);
            IResource resource = new StaticContentResource(streamString);
            IConfigurationInterpreter configurationInterpreter = new XmlInterpreter(resource);
            container = new WindsorContainer(configurationInterpreter);
        }

        private static string GetContent(string filePath)
        {
            FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader streamReader = new StreamReader(fileStream);

            string content = streamReader.ReadToEnd();
            return content;
        }

        public static TComponent Resolve<TComponent>()
        {
            return container.Resolve<TComponent>();
        }
    }
}
